<template>
  <div class="theme-manager">
    <!-- 主题选择器 -->
    <div class="theme-selector">
      <h3>🎨 界面主题</h3>
      <div class="theme-grid">
        <div 
          v-for="theme in themes" 
          :key="theme.id"
          class="theme-card"
          :class="{ active: currentTheme?.id === theme.id, owned: hasTheme(theme.id) }"
          @click="selectTheme(theme)"
        >
          <div class="theme-preview" :style="getThemeStyles(theme)">
            <div class="preview-content">
              <div class="preview-header">预览</div>
              <div class="preview-body">主题效果预览</div>
            </div>
          </div>
          <div class="theme-info">
            <h4>{{ theme.name }}</h4>
            <p>{{ theme.description }}</p>
            <div class="theme-price">
              <span v-if="theme.requiredAmount === 0">免费</span>
              <span v-else>{{ formatPrice(theme.requiredAmount) }} 积分</span>
            </div>
            <div v-if="!hasTheme(theme.id)" class="purchase-btn" @click.stop="purchaseTheme(theme)">
              购买
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 头像框选择器 -->
    <div class="avatar-frame-selector">
      <h3>🖼️ 头像框</h3>
      <div class="frame-grid">
        <div 
          v-for="frame in avatarFrames" 
          :key="frame.id"
          class="frame-card"
          :class="{ active: currentAvatarFrame?.id === frame.id, owned: hasAvatarFrame(frame.id) }"
          @click="selectAvatarFrame(frame)"
        >
          <div class="frame-preview">
            <img :src="frame.previewImage || '/default-avatar.png'" alt="头像" class="avatar-img">
            <div class="frame-overlay" :style="getFrameStyles(frame)"></div>
          </div>
          <div class="frame-info">
            <h4>{{ frame.name }}</h4>
            <p>{{ frame.description }}</p>
            <div class="frame-price">
              <span v-if="frame.requiredAmount === 0">免费</span>
              <span v-else>{{ formatPrice(frame.requiredAmount) }} 积分</span>
            </div>
            <div v-if="!hasAvatarFrame(frame.id)" class="purchase-btn" @click.stop="purchaseAvatarFrame(frame)">
              购买
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 聊天气泡选择器 -->
    <div class="chat-bubble-selector">
      <h3>💬 聊天气泡</h3>
      <div class="bubble-grid">
        <div 
          v-for="bubble in chatBubbles" 
          :key="bubble.id"
          class="bubble-card"
          :class="{ active: currentChatBubble?.id === bubble.id, owned: hasChatBubble(bubble.id) }"
          @click="selectChatBubble(bubble)"
        >
          <div class="bubble-preview" :style="getBubbleStyles(bubble)">
            <div class="bubble-content">消息预览</div>
          </div>
          <div class="bubble-info">
            <h4>{{ bubble.name }}</h4>
            <p>{{ bubble.description }}</p>
            <div class="bubble-price">
              <span v-if="bubble.requiredAmount === 0">免费</span>
              <span v-else>{{ formatPrice(bubble.requiredAmount) }} 积分</span>
            </div>
            <div v-if="!hasChatBubble(bubble.id)" class="purchase-btn" @click.stop="purchaseChatBubble(bubble)">
              购买
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 铭牌选择器 -->
    <div class="badge-selector">
      <h3>🏆 贡献者铭牌</h3>
      <div class="badge-grid">
        <div 
          v-for="badge in badges" 
          :key="badge.id"
          class="badge-card"
          :class="{ active: currentBadge?.id === badge.id, owned: hasBadge(badge.id) }"
          @click="selectBadge(badge)"
        >
          <div class="badge-preview" :style="getBadgeStyles(badge)">
            <img :src="badge.badgeIcon || '/default-badge.png'" alt="铭牌" class="badge-icon">
            <div class="badge-glow" v-if="badge.glowEffect"></div>
          </div>
          <div class="badge-info">
            <h4>{{ badge.name }}</h4>
            <p>{{ badge.description }}</p>
            <div class="badge-rarity" :class="badge.rarity">{{ getRarityText(badge.rarity) }}</div>
            <div class="badge-price">
              <span v-if="badge.requiredAmount === 0">免费</span>
              <span v-else>{{ formatPrice(badge.requiredAmount) }} 积分</span>
            </div>
            <div v-if="!hasBadge(badge.id)" class="purchase-btn" @click.stop="purchaseBadge(badge)">
              购买
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
import { getCurrentConfig } from '@/config/environment'

export default {
  name: 'ThemeManager',
  data() {
    return {
      themes: [],
      avatarFrames: [],
      chatBubbles: [],
      badges: [],
      currentTheme: null,
      currentAvatarFrame: null,
      currentChatBubble: null,
      currentBadge: null,
      userDecoration: null,
      loading: false
    }
  },
  
  mounted() {
    this.loadDecorationData()
  },
  
  methods: {
    // 加载装饰数据
    async loadDecorationData() {
      this.loading = true
      try {
        const config = getCurrentConfig()
        const [themesRes, framesRes, bubblesRes, badgesRes, userRes] = await Promise.all([
          axios.get(`${config.apiBaseUrl}/sponsor-decoration/themes`),
          axios.get(`${config.apiBaseUrl}/sponsor-decoration/avatar-frames`),
          axios.get(`${config.apiBaseUrl}/sponsor-decoration/chat-bubbles`),
          axios.get(`${config.apiBaseUrl}/sponsor-decoration/badges`),
          axios.get(`${config.apiBaseUrl}/sponsor-decoration/user/${this.$store.state.user.id}`)
        ])
        
        this.themes = themesRes.data.data || []
        this.avatarFrames = framesRes.data.data || []
        this.chatBubbles = bubblesRes.data.data || []
        this.badges = badgesRes.data.data || []
        this.userDecoration = userRes.data.data
        
        // 设置当前使用的装饰
        this.setCurrentDecorations()
        
      } catch (error) {
        console.error('加载装饰数据失败:', error)
        this.$toast.error('加载装饰数据失败')
      } finally {
        this.loading = false
      }
    },
    
    // 设置当前装饰
    setCurrentDecorations() {
      if (this.userDecoration) {
        this.currentTheme = this.themes.find(t => t.id === this.userDecoration.currentThemeId)
        this.currentAvatarFrame = this.avatarFrames.find(f => f.id === this.userDecoration.currentAvatarFrameId)
        this.currentChatBubble = this.chatBubbles.find(b => b.id === this.userDecoration.currentChatBubbleId)
        this.currentBadge = this.badges.find(b => b.id === this.userDecoration.currentBadgeId)
      }
    },
    
    // 选择主题
    async selectTheme(theme) {
      if (!this.hasTheme(theme.id)) {
        this.$toast.warning('您尚未拥有该主题')
        return
      }
      
      try {
        const config = getCurrentConfig()
        await axios.post(`${config.apiBaseUrl}/sponsor-decoration/user/${this.$store.state.user.id}/theme`, {
          themeId: theme.id
        })
        
        this.currentTheme = theme
        this.applyTheme(theme)
        this.$toast.success('主题已切换')
        
      } catch (error) {
        console.error('切换主题失败:', error)
        this.$toast.error('切换主题失败')
      }
    },
    
    // 选择头像框
    async selectAvatarFrame(frame) {
      if (!this.hasAvatarFrame(frame.id)) {
        this.$toast.warning('您尚未拥有该头像框')
        return
      }
      
      try {
        const config = getCurrentConfig()
        await axios.post(`${config.apiBaseUrl}/sponsor-decoration/user/${this.$store.state.user.id}/avatar-frame`, {
          frameId: frame.id
        })
        
        this.currentAvatarFrame = frame
        this.$toast.success('头像框已切换')
        
      } catch (error) {
        console.error('切换头像框失败:', error)
        this.$toast.error('切换头像框失败')
      }
    },
    
    // 选择聊天气泡
    async selectChatBubble(bubble) {
      if (!this.hasChatBubble(bubble.id)) {
        this.$toast.warning('您尚未拥有该聊天气泡')
        return
      }
      
      try {
        const config = getCurrentConfig()
        await axios.post(`${config.apiBaseUrl}/sponsor-decoration/user/${this.$store.state.user.id}/chat-bubble`, {
          bubbleId: bubble.id
        })
        
        this.currentChatBubble = bubble
        this.$toast.success('聊天气泡已切换')
        
      } catch (error) {
        console.error('切换聊天气泡失败:', error)
        this.$toast.error('切换聊天气泡失败')
      }
    },
    
    // 选择铭牌
    async selectBadge(badge) {
      if (!this.hasBadge(badge.id)) {
        this.$toast.warning('您尚未拥有该铭牌')
        return
      }
      
      try {
        const config = getCurrentConfig()
        await axios.post(`${config.apiBaseUrl}/sponsor-decoration/user/${this.$store.state.user.id}/badge`, {
          badgeId: badge.id
        })
        
        this.currentBadge = badge
        this.$toast.success('铭牌已切换')
        
      } catch (error) {
        console.error('切换铭牌失败:', error)
        this.$toast.error('切换铭牌失败')
      }
    },
    
    // 购买主题
    async purchaseTheme(theme) {
      await this.purchaseDecoration('theme', theme.id, theme.requiredAmount)
    },
    
    // 购买头像框
    async purchaseAvatarFrame(frame) {
      await this.purchaseDecoration('avatarFrame', frame.id, frame.requiredAmount)
    },
    
    // 购买聊天气泡
    async purchaseChatBubble(bubble) {
      await this.purchaseDecoration('chatBubble', bubble.id, bubble.requiredAmount)
    },
    
    // 购买铭牌
    async purchaseBadge(badge) {
      await this.purchaseDecoration('badge', badge.id, badge.requiredAmount)
    },
    
    // 购买装饰物品
    async purchaseDecoration(type, decorationId, amount) {
      if (amount === 0) {
        this.$toast.info('该装饰物品免费')
        return
      }
      
      const confirmed = await this.$confirm(`确定花费 ${this.formatPrice(amount)} 积分购买此装饰物品吗？`, '确认购买')
      if (!confirmed) return
      
      try {
        const config = getCurrentConfig()
        const response = await axios.post(`${config.apiBaseUrl}/sponsor-decoration/purchase`, {
          userId: this.$store.state.user.id,
          decorationType: type,
          decorationId: decorationId,
          amount: amount
        })
        
        if (response.data.success) {
          this.$toast.success('购买成功！')
          this.loadDecorationData() // 重新加载数据
        } else {
          this.$toast.error(response.data.message)
        }
        
      } catch (error) {
        console.error('购买失败:', error)
        this.$toast.error('购买失败')
      }
    },
    
    // 检查是否拥有主题
    hasTheme(themeId) {
      if (!this.userDecoration?.ownedThemes) return false
      try {
        const owned = JSON.parse(this.userDecoration.ownedThemes)
        return owned.includes(themeId)
      } catch {
        return false
      }
    },
    
    // 检查是否拥有头像框
    hasAvatarFrame(frameId) {
      if (!this.userDecoration?.ownedAvatarFrames) return false
      try {
        const owned = JSON.parse(this.userDecoration.ownedAvatarFrames)
        return owned.includes(frameId)
      } catch {
        return false
      }
    },
    
    // 检查是否拥有聊天气泡
    hasChatBubble(bubbleId) {
      if (!this.userDecoration?.ownedChatBubbles) return false
      try {
        const owned = JSON.parse(this.userDecoration.ownedChatBubbles)
        return owned.includes(bubbleId)
      } catch {
        return false
      }
    },
    
    // 检查是否拥有铭牌
    hasBadge(badgeId) {
      if (!this.userDecoration?.ownedBadges) return false
      try {
        const owned = JSON.parse(this.userDecoration.ownedBadges)
        return owned.includes(badgeId)
      } catch {
        return false
      }
    },
    
    // 应用主题
    applyTheme(theme) {
      if (theme.cssStyles) {
        try {
          const styles = JSON.parse(theme.cssStyles)
          const root = document.documentElement
          
          Object.keys(styles).forEach(key => {
            root.style.setProperty(`--${key}`, styles[key])
          })
        } catch (error) {
          console.error('应用主题样式失败:', error)
        }
      }
    },
    
    // 获取主题样式
    getThemeStyles(theme) {
      if (!theme.cssStyles) return {}
      try {
        return JSON.parse(theme.cssStyles)
      } catch {
        return {}
      }
    },
    
    // 获取头像框样式
    getFrameStyles(frame) {
      // 根据头像框配置返回样式
      return {
        backgroundImage: frame.frameImage ? `url(${frame.frameImage})` : 'none',
        animation: frame.animationEffect || 'none'
      }
    },
    
    // 获取聊天气泡样式
    getBubbleStyles(bubble) {
      if (!bubble.cssStyles) return {}
      try {
        return JSON.parse(bubble.cssStyles)
      } catch {
        return {}
      }
    },
    
    // 获取铭牌样式
    getBadgeStyles(badge) {
      if (!badge.cssStyles) return {}
      try {
        return JSON.parse(badge.cssStyles)
      } catch {
        return {}
      }
    },
    
    // 格式化价格
    formatPrice(amount) {
      return (amount / 100).toFixed(2)
    },
    
    // 获取稀有度文本
    getRarityText(rarity) {
      const rarityMap = {
        'common': '普通',
        'rare': '稀有',
        'epic': '史诗',
        'legendary': '传奇'
      }
      return rarityMap[rarity] || '普通'
    }
  }
}
</script>

<style scoped>
.theme-manager {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.theme-selector,
.avatar-frame-selector,
.chat-bubble-selector,
.badge-selector {
  margin-bottom: 40px;
}

h3 {
  color: var(--primary-color, #007bff);
  margin-bottom: 20px;
  font-size: 1.5em;
}

.theme-grid,
.frame-grid,
.bubble-grid,
.badge-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 20px;
}

.theme-card,
.frame-card,
.bubble-card,
.badge-card {
  border: 2px solid #e0e0e0;
  border-radius: 12px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.3s ease;
  background: white;
}

.theme-card:hover,
.frame-card:hover,
.bubble-card:hover,
.badge-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0,0,0,0.1);
}

.theme-card.active,
.frame-card.active,
.bubble-card.active,
.badge-card.active {
  border-color: var(--primary-color, #007bff);
  box-shadow: 0 0 0 2px rgba(0,123,255,0.25);
}

.theme-card.owned,
.frame-card.owned,
.bubble-card.owned,
.badge-card.owned {
  border-color: #28a745;
}

.theme-preview {
  height: 120px;
  border-radius: 8px;
  margin-bottom: 12px;
  overflow: hidden;
  position: relative;
}

.preview-content {
  padding: 16px;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.preview-header {
  font-weight: bold;
  font-size: 14px;
}

.preview-body {
  font-size: 12px;
  opacity: 0.8;
}

.frame-preview {
  position: relative;
  width: 80px;
  height: 80px;
  margin: 0 auto 12px;
  border-radius: 50%;
  overflow: hidden;
}

.avatar-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.frame-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
}

.bubble-preview {
  height: 60px;
  border-radius: 20px;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.bubble-content {
  padding: 8px 16px;
  font-size: 14px;
}

.badge-preview {
  position: relative;
  width: 60px;
  height: 60px;
  margin: 0 auto 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.badge-icon {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.badge-glow {
  position: absolute;
  top: -5px;
  left: -5px;
  right: -5px;
  bottom: -5px;
  border-radius: 50%;
  background: linear-gradient(45deg, #ff6b6b, #4ecdc4, #45b7d1);
  animation: glow 2s ease-in-out infinite alternate;
}

@keyframes glow {
  from { opacity: 0.5; }
  to { opacity: 1; }
}

.theme-info,
.frame-info,
.bubble-info,
.badge-info {
  text-align: center;
}

h4 {
  margin: 0 0 8px 0;
  font-size: 1.1em;
  color: #333;
}

p {
  margin: 0 0 12px 0;
  color: #666;
  font-size: 0.9em;
  line-height: 1.4;
}

.theme-price,
.frame-price,
.bubble-price,
.badge-price {
  font-weight: bold;
  color: var(--primary-color, #007bff);
  margin-bottom: 8px;
}

.badge-rarity {
  padding: 4px 8px;
  border-radius: 12px;
  font-size: 0.8em;
  font-weight: bold;
  margin-bottom: 8px;
  display: inline-block;
}

.badge-rarity.common {
  background: #6c757d;
  color: white;
}

.badge-rarity.rare {
  background: #17a2b8;
  color: white;
}

.badge-rarity.epic {
  background: #6f42c1;
  color: white;
}

.badge-rarity.legendary {
  background: linear-gradient(45deg, #ffd700, #ffed4e);
  color: #333;
}

.purchase-btn {
  background: var(--primary-color, #007bff);
  color: white;
  padding: 8px 16px;
  border-radius: 6px;
  font-size: 0.9em;
  cursor: pointer;
  transition: background 0.3s ease;
}

.purchase-btn:hover {
  background: var(--primary-color-dark, #0056b3);
}
</style>

